/**

 @Name：用户管理
 @Author：xuqipeter
 @Site：http://blog.xuziyu.cn/
    
 */

layui.define(function (exports) {
  layui.use(['table', 'layer', 'm-utils', 'form', 'admin', 'view', 'AgentModel'], function () {
    var $ = layui.$,
      table = layui.table,
      layer = layui.layer,
      admin = layui.admin,
      view = layui.view,
      form = layui.form,
      setter = layui.setter,
      baseUrl = setter.baseUrl,
      utils = layui['m-utils'],
      agentModel = layui.AgentModel;
    form.render();
    agentModel.tableList = agentModel._tableList('总代')
    /* search重载table */
    form.on('submit(js-btn-table-search)', function (data) {
      let field = data.field;
      agentModel.tableList.reload({
        where: field
      });
      return false;
    });
    table.on('toolbar(tableList)', function (obj) {
      switch (obj.event) {
        case 'add':
          _layerAdd();
          layer.full(agentModel.layerFull);
          break;
      }
    });
    /*监听主页面table行工具栏*/
    var tableObj;
    table.on('tool(tableList)', function (obj) {
      tableObj = obj;
      const data = obj.data;
      const layEvent = obj.event;
      const tr = obj.tr;
      switch (layEvent) {
        case 'showOrders':
          _layerShowOrders(tableObj);
          layer.full(agentModel.layerFull);
          break;
        case 'edit':
          _layerEdit(tableObj);
          layer.full(agentModel.layerFull);
          break;
        case 'del':
          _layerDel(tableObj);
      }
    });
    /* 查看购买记录 */
    function _layerShowOrders(obj) {
      const data = obj.data
      agentModel.layerFull = admin.popup({
        id: 'detail',
        title: `用户：<b>${data.name}</b> 的购买记录`,
        area: ['800px', '400px'],
        offset: 't',
        btn: ['关闭'],
        btnAlign: 'c',
        maxmin: true,
        skin: 'myskin',
        success: function () {
          view(this.id).render('g-popup/agent__order_list').done(function () {
            agentModel._groupMemberOrderList(data)
          })
        }
      });
    }
  })

  exports('m-agent__main', {})
})